package servlets;

import ejb.facades.TripFacadeLocal;
import entities.Trip;
import entities.User;
import java.io.IOException;
import javax.ejb.EJB;
import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import static servlets.ServletBase.JSP_INVALID_ARGUMENTS;
import sessions.IUserAccountSession;
import util.BreadCrumbItem;
import util.Script;
import util.Validator;

/**
 * Trabalho PD - 2014/2015
 * @author a21210380 a21210392
 */
@WebServlet("/evaluate.html")
public class EvaluateServlet extends ServletBase {
    private final static BreadCrumbItem[] BREAD_CRUMB_ITEMS = {
        new BreadCrumbItem("/Trabalho_PD/homepage.html", "Homepage", false),
        new BreadCrumbItem("/Trabalho_PD/evaluate.html", "Evaluate", true)};
    
    private final static Script[] EXTRA_SCRIPTS = { 
        new Script("resources/js/webserviceboleias.js", true), 
        new Script("resources/js/myvalidator.js", true),
        new Script("resources/js/customjs/evaluate.js", true),
        new Script("resources/js/webserviceemail.js", true)};
    
    @Inject
    private IUserAccountSession userAccountSession;
    
    @EJB
    private TripFacadeLocal tripFacade;
    
    public EvaluateServlet() {
        super("Evaluate", BREAD_CRUMB_ITEMS, null, EXTRA_SCRIPTS);
    }
    
    @Override
    protected void onGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (userAccountSession.isVisitor()) {
            callJsp(JSP_NOT_AUTHORIZED, req, resp);
            return;
        }
        String parameter = req.getParameter("id");
        if(parameter == null) {
            callJsp(JSP_INVALID_ARGUMENTS, req, resp);
            return;
        }
        Integer parseInt = Validator.parseInt(parameter);
        if(parseInt == null) {
            callJsp(JSP_INVALID_ARGUMENTS, req, resp);
            return;
        }
        User user = userAccountSession.getUser();
        if(user == null) {
            callJsp(JSP_NOT_AUTHORIZED, req, resp);
            return;
        }
        Trip find = tripFacade.find(parseInt);
        if(find == null) {
            callJsp(JSP_INVALID_ARGUMENTS, req, resp);
            return;
        }
        if(!find.isEnded()) {
            callJsp(JSP_INVALID_ARGUMENTS, req, resp);
            return;
        }
        if(find.didUserEvaluatedThisTrip(user)) {
            callJsp(JSP_INVALID_ARGUMENTS, req, resp);
            return;
        }
        req.setAttribute("loggedUser", user);
        req.setAttribute("trip", find);
        req.setAttribute("car", find.getVehicle());
        if(user.havesVehicle(find.getVehicle())) {
            req.setAttribute("driver", true);
        } else {
            req.setAttribute("driver", false);
        }
        
        callJsp(JSP_EVALUATE, req, resp);
    }
    
    @Override
    protected void onPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (userAccountSession.isVisitor()) {
            callJsp(JSP_NOT_AUTHORIZED, req, resp);
            return;
        }
        String parameter = req.getParameter("id");
        if(parameter == null) {
            callJsp(JSP_INVALID_ARGUMENTS, req, resp);
            return;
        }
        Integer parseInt = Validator.parseInt(parameter);
        if(parseInt == null) {
            callJsp(JSP_INVALID_ARGUMENTS, req, resp);
            return;
        }
        User user = userAccountSession.getUser();
        if(user == null) {
            callJsp(JSP_NOT_AUTHORIZED, req, resp);
            return;
        }
        Trip find = tripFacade.find(parseInt);
        if(find == null) {
            callJsp(JSP_INVALID_ARGUMENTS, req, resp);
            return;
        }
        if(!find.isEnded()) {
            callJsp(JSP_INVALID_ARGUMENTS, req, resp);
            return;
        }
        req.setAttribute("loggedUser", user);
        req.setAttribute("trip", find);
        callJsp(JSP_EVALUATE, req, resp);
    }

}
